package com.inspur.icity.bus.restapi;

import com.inspur.icity.bus.api.user.enumeration.AreaCodeEnum;
import com.inspur.icity.bus.api.user.model.BusAddress;
import com.inspur.icity.bus.api.user.model.BusBaseData;
import com.inspur.icity.bus.api.user.service.BusAddressService;
import com.inspur.icity.bus.api.user.service.BusBaseDataService;
import com.inspur.icity.bus.base.core.model.BaseResult;
import com.inspur.icity.bus.base.core.util.GaodeMapApi;
import com.inspur.icity.bus.base.core.web.BaseController;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设置起始点、查询起始点
 */
@Controller
@RequestMapping(value = "/address")
public class BusAddressController extends BaseController {

    @Autowired
    private BusAddressService busAddressService;

    @Autowired
    private BusBaseDataService busBaseDataService;


    /**
     * 获取用户起点、终点搜索历史列表（限制10条）
     * @param mobile 登陆用户
     * @param addressType 地点类型 0：起点 ; 1：终点
     * @return
     */
    @RequestMapping(value = "/getUserAddress",method = RequestMethod.GET)
    @ResponseBody
    public Object getUserStartOrEndAddress(@RequestParam(value = "mobile",required = true)String mobile,
                                           @RequestParam(value = "city",required = false)String city,
                                           @RequestParam(value = "addressType",required = true)Integer addressType){
        List<BusAddress> list = busAddressService.getUserStartOrEndAddress(mobile,addressType,city);
        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String,Object> map = null;
        for(BusAddress busAddress: list){
            map = new HashMap<>();
            map.put("lng",busAddress.getLng());
            map.put("lat",busAddress.getLat());
            map.put("address",busAddress.getAddress());
            map.put("type",busAddress.getType());
            map.put("city",busAddress.getCity());
            if(null != busAddress.getCity() && !"".equals(busAddress.getCity())){
                map.put("areacode", AreaCodeEnum.getAreaCode(busAddress.getCity()));
            }
            resultList.add(map);
        }
        return BaseResult.success(resultList);
    }


    /**
     * 查询起点、终点位置信息（搜索框调用）
     * @param keywords
     * @param city
     * @param areacode
     * @return
     */
    @RequestMapping(value = "/queryAddress",method = RequestMethod.GET)
    @ResponseBody
    public Object queryAddress(@RequestParam(value = "keywords", required = true)String keywords,
                               @RequestParam(value = "city", required = true)String city,
                               @RequestParam(value = "areacode", required = true)String areacode){
        return BaseResult.success(busAddressService.queryAddress(keywords,city,areacode));
    }


    /**
     * 检测起始点距离
     * @return
     */
    @RequestMapping(value = "/detectStartEndAddress",method = RequestMethod.GET)
    @ResponseBody
    public Object detectStartEndAddress(String startLng, String startLat, String endLng, String endLat, String city){
        //查询起始点距离
        double distance = GaodeMapApi.getDistance(startLng, startLat,endLng,endLat);
        if(distance <= 500){
            return BaseResult.fail("起点和终点位置太近");
        }
        return BaseResult.success(null);
    }


    /**
     * 乘车方案查询
     * @return
     */
    @RequestMapping(value = "/queryBusPlan",method = RequestMethod.POST)
    @ResponseBody
    public Object queryBusPlan(@RequestBody Map routeMap){
        String mobile = routeMap.get("mobile") != null ? routeMap.get("mobile").toString() : "";
        String startAddress = routeMap.get("startAddress") != null ? routeMap.get("startAddress").toString() : "";
        String startLng = routeMap.get("startLng") != null ? routeMap.get("startLng").toString() : "";
        String startLat = routeMap.get("startLat") != null ? routeMap.get("startLat").toString() : "";
        int startType = routeMap.get("startType") != null ? Integer.parseInt(routeMap.get("startType").toString()) : 0;
        String endAddress = routeMap.get("endAddress") != null ? routeMap.get("endAddress").toString() : "";
        String endLng = routeMap.get("endLng") != null ? routeMap.get("endLng").toString() : "";
        String endLat = routeMap.get("endLat") != null ? routeMap.get("endLat").toString() : "";
        int endType = routeMap.get("endType") != null ? Integer.parseInt(routeMap.get("endType").toString()) : 0;
        String strategy = routeMap.get("strategy") != null ? routeMap.get("strategy").toString() : "";
        String city = routeMap.get("city") != null ? routeMap.get("city").toString():"";
        List<Map<String,Object>> routePlans = busAddressService.queryBusPlan(mobile,startAddress,new Double(startLng),new Double(startLat),startType,endAddress,new Double(endLng),new Double(endLat),endType,Integer.parseInt(strategy),city);
        if(routePlans == null || routePlans.size() == 0){
            return BaseResult.fail("没有搜索到数据！");
        }
        return BaseResult.success(routePlans);
    }


    /**
     * 方案详情查询
     * @return
     */
    @RequestMapping(value = "/queryBusPlanDetail",method = RequestMethod.POST)
    @ResponseBody
    public Object queryBusPlanDetail(@RequestBody Map routeMap){
        if(null == routeMap.get("detail")){
            return null;
        }
        List<Map<String,Object>> detail = (List)routeMap.get("detail");
        String areacode = "";
        String positionCity = "";
        if(null != routeMap.get("areacode") && !"".equals(routeMap.get("areacode"))){ //用户选择城市
            areacode = routeMap.get("areacode").toString();
        }
//        if(null != routeMap.get("positionCity") && !"".equals(routeMap.get("positionCity"))){ //设备定位城市
//            positionCity = routeMap.get("positionCity").toString();
//        }
//        Map<String,Object> map = new HashMap<>();
//        map.put("code",areacode);
//        List<BusBaseData> list = busBaseDataService.find(map);
//        if(null != list && list.size() != 0){
//            if(!positionCity.equals(list.get(0).getCity())){ //用户选择城市和定位城市不符合时
//                return BaseResult.success(detail);
//            }
//        }
        List<Map<String,Object>> planDetail  = busAddressService.queryBusPlanDetail(detail,areacode);
        if(planDetail == null || planDetail.size() == 0){
            return BaseResult.fail(null);
        }
        return BaseResult.success(planDetail);
    }
}
